//
//  DrawArcView.swift
//  L02DrawShapes
//
//  Created by liy on 2024/12/24.
//	圆弧 arc
// 	画曲线 curve或者quadCurve

import UIKit

class DrawArcView: UIView {
  override func draw(_ rect: CGRect) {
    let ct = UIGraphicsGetCurrentContext()

    // 画个填充的半圆
    ct?.addArc(center:
      CGPoint(x: 150,
              y: 300),
      radius: 100,
      startAngle: 0,
      endAngle: .pi,
      clockwise: false)
    ct?.setFillColor(red: 0, green: 0, blue: 0.8, alpha: 1)
    ct?.fillPath()

    // 画个圆弧(注意代码先后顺序导致的层叠遮挡)
    ct?.addArc(center: CGPoint(x: 150, y: 300), radius: 100, startAngle: 0, endAngle: .pi * 2, clockwise: false)
    ct?.setLineWidth(10)
    ct?.setStrokeColor(UIColor.red.cgColor)
    ct?.strokePath()

    // 画个椭圆
    // 矩形是椭圆，正方形是正圆
    ct?.addEllipse(in: CGRect(x: 100, y: 500, width: 200, height: 100))
    ct?.setStrokeColor(UIColor.orange.cgColor)
    ct?.strokePath()
    // stroke后就没有path可用，后面的fillPath无效
    ct?.setFillColor(UIColor.purple.cgColor)
    ct?.fillPath()

    // 画曲线（二次贝瑟尔曲线或者三次贝瑟尔曲线)
    ct?.move(to: CGPointMake(50, 900))
    ct?.addQuadCurve(to: CGPointMake(100, 900), control: CGPointMake(200, 400))
    ct?.addCurve(to: CGPointMake(400, 900), control1: CGPointMake(200, 500), control2: CGPointMake(400, 700))
    ct?.setStrokeColor(UIColor.blue.cgColor)
    ct?.strokePath()
  }
}

#if DEBUG

  import SwiftUI

  struct DrawArcViewRepresentable: UIViewRepresentable {
    typealias UIViewType = DrawArcView

    func makeUIView(context: Context) -> UIViewType {
      let a = DrawArcView()
      a.backgroundColor = .white
      return a
    }

    func updateUIView(_ uiView: UIViewType, context: Context) {
    }
  }

  struct DrawArcView_Previews: PreviewProvider {
    static var previews: some View {
      DrawArcViewRepresentable()
    }
  }

#endif
